var positions = null;
//返回随机颜色
function getRandomColor() {
    return new Cesium.Color(Math.random(), Math.random(), Math.random());
}

function floodlaond() {
    var handler_pick
    $("#pick_height").click(function(){
        var scene = viewer.scene;
        var tooltip = new GlobeTooltip(viewer.container);
        if (!handler_pick || handler_pick.isDestroyed()) {
            handler_pick = new Cesium.ScreenSpaceEventHandler(scene.canvas);
            //设置鼠标左键单击回调事件
            var position = null;
            handler_pick.setInputAction(function(e) {
                //首先移除之前添加的点
                removeAllEntities();
                //获取点击位置笛卡尔坐标
                position = getPickPositionGlobe(e.position);
                //将笛卡尔坐标转化为经纬度坐标
                var cartographic = Cesium.Cartographic.fromCartesian(position);
                var longitude = Cesium.Math.toDegrees(cartographic.longitude);
                var latitude = Cesium.Math.toDegrees(cartographic.latitude);
                var height = cartographic.height;
                if (height < 0) {
                    height = 0;
                }
                //创建弹出框信息
                var val = Number(height).toFixed(2).toString();
                var html = "高度值："+val+" 米"
                $("#height_val").html(html);
                //在点击位置添加对应点
                viewer.entities.add({
                    point: {
                        color: Cesium.Color.RED.withAlpha(1),
                        pixelSize: 10,
                        outlineColor: Cesium.Color.WHITE.withAlpha(1)
                    },
                    position: Cesium.Cartesian3.fromDegrees(longitude, latitude, height + 0.5)
                });
                handler_pick ? handler_pick.destroy() : null
                tooltip.setVisible(false);
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
            //鼠标移动
            handler_pick.setInputAction(function(event) {
                var wp = event.endPosition;
                if (!Cesium.defined(wp)) {
                    return;
                }
                if (position == null) {
                    tooltip.showAt(wp, "<p>单击拾取高程点</p>");
                }else{
                    tooltip.setVisible(false);
                }
                var cartesian = getPickPositionGlobe(wp);
                if (!Cesium.defined(cartesian)) {
                    return;
                }
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        }
    })
    $("#flood_draw").click(function() {
        handler_pick ? handler_pick.destroy() : null
        tracker.trackPolygon(function(position) {
            var array = [].concat(position);
            positions = [];
            //var mycars = [];
            for (var i = 0, len = array.length; i < len; i++) {
                var cartographic = Cesium.Cartographic.fromCartesian(array[i]);
                var longitude = Cesium.Math.toDegrees(cartographic.longitude);
                var latitude = Cesium.Math.toDegrees(cartographic.latitude);
                var h = cartographic.height;
                //mycars.push(h);
                if (positions.indexOf(longitude) == -1 && positions.indexOf(latitude) == -1) {
                    positions.push(longitude);
                    positions.push(latitude);
                    positions.push(h);
                }
            }
            //计算最小最大高度
            // $("#maxHeight").val(Math.max.apply(Math, mycars));
            // $("#minHeight").val(Math.min.apply(Math, mycars));
        });
    })
    $("#flood_begin").click(function() {
        if (!positions) {
            showMsgLayer("请先绘制区域！");
            return;
        }
        removeAllEntities();
        var maxValue = Math.ceil(document.getElementById("maxHeight").value);
        var minValue = Math.floor(document.getElementById("minHeight").value);
        var speed = parseFloat($("#flood_speed").val());
        _drawWater(maxValue, positions, minValue, speed)
    })
    $("#flood_clear").click(function() {
        flood_clear();
    })

    function _drawWater(targetHeight, adapCoordi, minValue, speed) {        viewer.entities.add({
            polygon: {
                hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(adapCoordi)),
                material: Cesium.Color.BLUE.withAlpha(0.6),
                perPositionHeight: true,
                extrudedHeight: new Cesium.CallbackProperty(function() {
                    minValue += (speed / 10);    
                    if (minValue > targetHeight) {
                        minValue = targetHeight;
                    }    
                    return minValue
                }, false),
               // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
            }
        });
    }
}

function flood_clear(clear) {
    positions = null;
    if (!clear) { removeAllEntities(); }
}